#include "sdk.h"
#include "virtualbuildtargetsdlg.h"
#include "globals.h"
#include "wx/textdlg.h"
#include "wx/utils.h"
#include <wx/xrc/xmlres.h>

BEGIN_EVENT_TABLE( VirtualBuildTargetsDlg, wxScrollingDialog )
  EVT_UPDATE_UI( -1, VirtualBuildTargetsDlg::OnUpdateUI )
END_EVENT_TABLE()

VirtualBuildTargetsDlg::VirtualBuildTargetsDlg( wxWindow* parent, wxWindowID, M_Code_Project* project ) : m_pProject( project ) {
  wxXmlResource* xml_res = wxXmlResource::Get();
  wxString resPath = ConfigManager::GetDataFolder() + _T( "/配置虚拟目标.xrc" );
  if( !xml_res->Load( resPath ) )  {
    xml_File_lock = false;
    Manager::Get()->GetLogManager()->LogInf( F( _T( "文件丢失: %s " ), resPath.wx_str() ) );
    return;
  }
  if( !xml_res->LoadObject( this, parent, _T( "VirtualBuildTargetsDlg" ), _T( "wxScrollingDialog" ) ) ) {
    xml_File_lock = false;
    Manager::Get()->GetLogManager()->LogInf( _T( "资源丢失" ) );
    return;
  }
  xml_File_lock = true;
  lstAliases = ( wxListBox* )FindWindow( XRCID( "ID_LST_ALIASES" ) );
  btnAdd = ( wxButton* )FindWindow( XRCID( "ID_BTN_ADD" ) );
  btnEdit = ( wxButton* )FindWindow( XRCID( "ID_BTN_EDIT" ) );
  btnRemove = ( wxButton* )FindWindow( XRCID( "ID_BTN_REMOVE" ) );
  lstTargets = ( wxCheckListBox* )FindWindow( XRCID( "ID_LST_TARGETS" ) );
  Connect( XRCID( "ID_LST_ALIASES" ), wxEVT_COMMAND_LISTBOX_SELECTED, ( wxObjectEventFunction )&VirtualBuildTargetsDlg::OnAliasesSelect );
  Connect( XRCID( "ID_BTN_ADD" ), wxEVT_COMMAND_BUTTON_CLICKED, ( wxObjectEventFunction )&VirtualBuildTargetsDlg::OnAddClick );
  Connect( XRCID( "ID_BTN_EDIT" ), wxEVT_COMMAND_BUTTON_CLICKED, ( wxObjectEventFunction )&VirtualBuildTargetsDlg::OnEditClick );
  Connect( XRCID( "ID_BTN_REMOVE" ), wxEVT_COMMAND_BUTTON_CLICKED, ( wxObjectEventFunction )&VirtualBuildTargetsDlg::OnRemoveClick );
  Connect( XRCID( "ID_LST_TARGETS" ), wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, ( wxObjectEventFunction )&VirtualBuildTargetsDlg::OnTargetsToggled );
  XRCCTRL( *this, "wxID_OK", wxButton )->SetDefault();
  wxArrayString virtuals = m_pProject->GetVirtualBuildTargets();
  lstAliases->Set( virtuals );
  if( lstAliases->GetCount() > 0 ) {
    lstAliases->SetSelection( 0 );
  }
  for( int i = 0; i < m_pProject->GetBuildTargetsCount(); ++i ) {
    lstTargets->Append( m_pProject->GetBuildTarget( i )->GetTitle() );
  }
  CheckTargets();
}

VirtualBuildTargetsDlg::~VirtualBuildTargetsDlg() {}

void VirtualBuildTargetsDlg::SetVirtualTarget( const wxString& targetName ) {
  if( !lstTargets->IsEnabled() || lstTargets->IsEmpty() ) {
    return;
  }
  wxArrayString checked;
  for( size_t i = 0; i < lstTargets->GetCount(); ++i ) {
    if( lstTargets->IsChecked( i ) ) {
      checked.Add( lstTargets->GetString( i ) );
    }
  }
  if( checked.GetCount() == 0 ) {
    msging( _T( "此虚拟生成目标无效.\n"
                "虚拟目标必须至少有一个活动目标.\n"
                "是否要删除虚拟生成目标?" ), _T( "错误" ), wxICON_ERROR, this );
  } else if( checked.GetCount() > 0 ) {
    if( !m_pProject->DefineVirtualBuildTarget( targetName, checked ) ) {
      msging( _T( "无法设置此虚拟生成目标 " ), _T( "错误" ), wxICON_ERROR, this );
    }
  }
}

void VirtualBuildTargetsDlg::CheckTargets() {
  const wxArrayString& group = m_pProject->GetVirtualBuildTargetGroup( lstAliases->GetStringSelection() );
  for( int i = 0; i < m_pProject->GetBuildTargetsCount(); ++i ) {
    wxString tgtName = m_pProject->GetBuildTarget( i )->GetTitle();
    bool check = group.Index( tgtName ) != wxNOT_FOUND;
    lstTargets->Check( i, check );
  }
}

void VirtualBuildTargetsDlg::OnUpdateUI( M_Code_unused wxUpdateUIEvent& event ) {
  bool hasSel = lstAliases->GetSelection() != -1;
  btnEdit->Enable( hasSel );
  btnRemove->Enable( hasSel );
  lstTargets->Enable( hasSel );
}

void VirtualBuildTargetsDlg::OnAddClick( M_Code_unused wxCommandEvent& event ) {
  wxString targetName = cbGetTextFromUser( _T( "输入新的虚拟生成目标名称:" ), _T( "新建虚拟生成目标" ) );
  if( targetName.IsEmpty() ) {
    return;
  }
  if( lstAliases->FindString( targetName, true ) != wxNOT_FOUND ) {
    msging( _T( "此项目中已存在具有此名称的虚拟生成目标!" ), _T( "错误" ),
            wxOK | wxCENTRE | wxICON_ERROR, this );
    return;
  }
  if( m_pProject->GetBuildTarget( targetName ) ) {
    msging( _T( "此项目中已存在具有此名称的实际生成目标!" ), _T( "错误" ),
            wxOK | wxCENTRE | wxICON_ERROR, this );
    return;
  }
  lstAliases->Append( targetName );
  lstAliases->SetSelection( lstAliases->GetCount() - 1 );
  CheckTargets();
}

void VirtualBuildTargetsDlg::OnEditClick( M_Code_unused wxCommandEvent& event ) {
  wxString targetName = cbGetTextFromUser( _T( "输入新的虚拟生成目标名称:" ), _T( "编辑虚拟生成目标" ),
                        lstAliases->GetStringSelection() );
  if( targetName.IsEmpty() || targetName == lstAliases->GetStringSelection() ) {
    return;
  }
  if( lstAliases->FindString( targetName, true ) != wxNOT_FOUND ) {
    msging( _T( "此项目中已存在具有此名称的虚拟生成目标!" ), _T( "错误" ), wxOK | wxCENTRE | wxICON_ERROR, this );
    return;
  }
  if( m_pProject->GetBuildTarget( targetName ) ) {
    msging( _T( "此项目中已存在具有此名称的实际生成目标!" ), _T( "错误" ), wxOK | wxCENTRE | wxICON_ERROR, this );
    return;
  }
  m_pProject->RemoveVirtualBuildTarget( lstAliases->GetStringSelection() );
  lstAliases->SetString( lstAliases->GetSelection(), targetName );
  SetVirtualTarget( targetName );
}

void VirtualBuildTargetsDlg::OnRemoveClick( M_Code_unused wxCommandEvent& event ) {
  if( msging( _T( "是否确实要删除此虚拟生成目标?" ), _T( "审查" ), wxYES_NO | wxICON_QUESTION, this ) == wxID_NO ) {
    return;
  }
  m_pProject->RemoveVirtualBuildTarget( lstAliases->GetStringSelection() );
  lstAliases->Delete( lstAliases->GetSelection() );
  lstAliases->SetSelection( 0 );
  CheckTargets();
}

void VirtualBuildTargetsDlg::OnAliasesSelect( M_Code_unused wxCommandEvent& event ) {
  CheckTargets();
}

void VirtualBuildTargetsDlg::OnTargetsToggled( M_Code_unused wxCommandEvent& event ) {
  SetVirtualTarget( lstAliases->GetStringSelection() );
}
